<?php

namespace app\modules\mall\controllers;

use Yii;
use app\controllers\ApiController;
use app\modules\mall\models\Flash;
use app\modules\mall\models\Goods;
class FlashController extends ApiController
{
    
    const DEFAULT_SIZE = 2;
    const FLASH_TYPE = 2;
    
    /**
     * 闪购首页
     * GET
     */
    public function actionIndex() {
        $data = [];
        $data['page'] = $this->_getPage();
        $data['size'] = self::DEFAULT_SIZE;
        
        $flashModel = new Flash();
        $list = $flashModel->goodsList($data);
        $this->_exit(0, $list); 
    }
    
    /**
     * 闪购商品详情
     * GET
     */
    public function actionGoodsdetail() {
        $goodsid    = $this->_validateNum(trim(Yii::$app->request->get('goodsid')));
        
        $flashModel = new Flash();
        $goodsModel = new Goods();
        
        $info = $flashModel->getGoodsInfo($goodsid);
        if(empty($info)) {
            $this->_exit(40010);
        }
        //信息数
        $info['hasMsg'] = '1';
        
        //商店信息
        $shopInfo = $this->cget(Yii::$app->params['mall_domain'].'/mall/order/getshopinfo?sid=' . $info['sid']);
        $info['shopname'] = $shopInfo['shop_name'];
        $info['shopContent'] = $shopInfo['content'];
        
        $info['isShippingFree'] = $this->cget(Yii::$app->params['mall_domain'].'/mall/order/isshopfree?goodsid=' . $goodsid);
        
        //商品最大价，最小价
        $info['maxPrice'] = $flashModel->getGoodsMaxPrice($goodsid);
        $info['minPrice'] = $flashModel->getGoodsMinPrice($goodsid);
        $info['location'] = $this->ip2Location();
        //查看商品评论信息
        $tmp = [];
        $tmp['goods_id'] = $goodsid;
        $tmp['type'] = self::FLASH_TYPE;
        $tmp['page'] = 1;
        $tmp['size'] = 1;
        $comment = $goodsModel->getCommentList($tmp);
        if(!empty($comment)) {
            $uid = $comment[0]['uid'];
            $user = $this->cget(Yii::$app->params['passport_domain'].'ucenter/finduserinformation?uid='.$uid);
            $user = json_decode($user,true);
            $comment[0]['avatar'] = $user['data']['avatar'];
            $comment[0]['username'] = $user['data']['nickname'];
            $info['comment'] = $comment[0];
        } else {
            $info['comment'] = [];
        }
        
        //商品是否收藏
        if($this->isLogin()) {
            $uid = $this->_getUid();
            $tmp = [];
            $tmp['uid'] = $uid;
            $tmp['goodsid'] = $goodsid;
            $tmp['type'] = self::FLASH_TYPE;
            $info['isCollect'] = $goodsModel->isCollect($tmp);
        } else {
            $info['isCollect'] = 0;
        }
        
        $sku = $flashModel->hasSku($goodsid);
        
        $arr = [];
        if($sku) {
            $colors = $flashModel->getSkucolor($goodsid);
            $arr['color'] = [];
            foreach($colors as $key=>$value) {
                $arr['color'][$key]['color'] = $value['color'];
                $arr['color'][$key]['exist'] = $flashModel->existColor($goodsid, $value['color']);
            }
        
            $sizes = $flashModel->getSkusize($goodsid);
            $arr['size'] = [];
            foreach($sizes as $key=>$value) {
                $arr['size'][$key]['size'] = $value['size'];
                $arr['size'][$key]['exist'] = $flashModel->existSize($goodsid, $value['size']);
            }
            $arr['skulist'] = $flashModel->getSkulist($goodsid);
        }
        $info['skulist'] = $arr;
        
        if(empty($info)) {
            $this->_exit(40010);
        } else {
            $this->_exit(0, $info);
        }
    }
    /**
     * 确认闪购订单
     */
    public function actionConfirmorder() {
        
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        
        $goodsInfo  = trim(Yii::$app->request->post('goodsInfo'));
        $goodsInfo  = json_decode($goodsInfo, true);
        if(!$goodsInfo) {
            $this->_exit(-1);
        }
        $goodsid    = $goodsInfo[0]['goodsid'];
        $skuid      = $goodsInfo[0]['skuid'];
        $num        = $goodsInfo[0]['num'];
        
        //获取想买商品的剩余库存
        $key = 'flash_' . $goodsid . '_' . $skuid;
        $userKey = 'flash_userlist_' . $goodsid . '_' . $skuid; 
        if($this->redis->hGet($userKey, $uid)) {
            $this->_exit(40023);
        }
        //$goodsNum = $this->redis->get($key);
        $goodsNum = 100;
        if($goodsNum > 0) {
            
            //redis 乐观锁
            $this->redis->watch($key);
            $this->redis->multi();
        
            //用户付款时需要判断缓存列表中是否有该用户id
            //加入用户重复提交，重复性判断
            $rob_result = 0;
            $this->redis->hSet($userKey, $uid, time());
            
            //库存减少
            $this->redis->decr($key);
            $rob_result = $this->redis->exec();
        
            if($rob_result) {
                //抢购成功，获取确认页面数据
                $this->_exit(0);
            } else {
                $this->_exit(40024);
            }
        } else {
            $this->_exit(40025);
        }
    }
    /**
     * 取消确认订单
     */
    public function actionCancel() {
        if($this->isLogin()) {
            $uid = $this->_getUid();
        } else {
            $this->_exit(40005);
        }
        $goodsid    = trim(Yii::$app->request->post('goodsid'));
        $skuid      = trim(Yii::$app->request->post('skuid'));
        $num        = trim(Yii::$app->request->post('num'));
        
        $key = 'flash_' . $goodsid . '_' . $skuid;
        $userKey = 'flash_userlist_' . $goodsid . '_' . $skuid;
        //$this->redis->watch($key);
        //$this->redis->multi();
        $this->redis->hDel($userKey, $uid);
        $this->redis->incr($key);
        //$this->redis->exec();
        $this->_exit(0);
    }
    
    //提交订单--调用直接下单的接口？？
    //付款--调用支付接口
    
}
